package com.learn.springboot.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.learn.springboot.dao.mapper.LiveTimeInfoMapper;
import com.learn.springboot.dao.mode.LiveTimeInfo;
import com.learn.springboot.dao.request.CommonPageResult;
import com.learn.springboot.dao.request.CommonPrimaryKeyRequest;
import com.learn.springboot.dao.request.LiveTimeInfoRequest;
import com.learn.springboot.dao.request.PageQueryRequest;
import com.learn.springboot.dao.request.ResponseManage;
import com.learn.springboot.dao.request.base.PrimaryKeyRequest;
import com.learn.springboot.dao.response.CommonResponse;
import com.learn.springboot.service.utils.PageUtils;

import cn.hutool.core.util.StrUtil;

/**
 * @author suzheng
 * @date 2024-05-17
 **/
@RestController
@RequestMapping("/api/live-time-info")
public class LiveTimeInfoController {
    @Autowired
    private LiveTimeInfoMapper liveTimeInfoMapper;

    @GetMapping("/list")
    public CommonResponse<CommonPageResult<LiveTimeInfo>> queryList(LiveTimeInfoRequest request){
        Page page = PageUtils.toPage(request);
        QueryWrapper queryWrapper = new QueryWrapper<>().eq(StrUtil.isNotBlank(request.getAnchor()),"anchor",request.getAnchor())
                .eq(StrUtil.isNotBlank(request.getAccount()),"account",request.getAccount())
                .ge(StrUtil.isNotBlank(request.getStartDt()),"startLiveTime",request.getStartDt())
                .ge(StrUtil.isNotBlank(request.getEndDt()),"endLiveTime",request.getEndDt());

        IPage<LiveTimeInfo> iPage  =liveTimeInfoMapper.selectPage(page,queryWrapper);
        CommonPageResult<LiveTimeInfo> commonPageResult =  PageUtils.toPageResult(page,iPage.getRecords());
        return ResponseManage.success(commonPageResult);
    }

    @PostMapping("/del")
    public CommonResponse queryList(@Valid CommonPrimaryKeyRequest<Integer> request){
        liveTimeInfoMapper.deleteById(request.getId());
        return ResponseManage.success();
    }
}
